package com.wzdigit.wms.basic.mapper.core.order.manager;

import com.wzdigit.framework.orm.mybatis.MyBatisRepository;
import com.wzdigit.wms.basic.domain.transfer.GdnDtlGoods;
import com.wzdigit.wms.basic.client.request.gdn.other.SearchOtherOutOrderRequest;
import com.wzdigit.wms.basic.client.response.gdn.other.OtherOutOrderInfoResponse;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@MyBatisRepository
public interface GdnManagerMapper {

    List<OtherOutOrderInfoResponse> selectOtherTaskInfo(SearchOtherOutOrderRequest input);

    @Select("SELECT COUNT(1) FROM WMS_GDN_HEADER WGH " +
            "INNER JOIN WMS_GDN_DTL WGD ON WGH.GDN_HEADER_ID = WGD.GDN_HEADER_ID " +
            "INNER JOIN WMS_PICKTASK_DTL WPD ON WGD.GDN_DTL_ID = WPD.DOC_DTL_ID " +
            "INNER JOIN WMS_PICKTASK_DTL_GOODS WPDG ON WPD.PICKTASK_DTL_ID = WPDG.PICKTASK_DTL_ID " +
            "WHERE WGH.GDN_NO = #{billNo} AND WPDG.GOODS_SN = #{sn}")
    int countPickTaskGoods(@Param("billNo") String billNo, @Param("sn") String sn);

    /**
     * 获取字符串中sn不在拣货清单中
     *
     * @param billNo 发货单
     * @param str    sn列表平凑成的字符串, 中间用','隔开
     * @return
     */
    @Select("WITH T AS ( " +
            "SELECT REGEXP_SUBSTR(#{str},'[^,]+', 1, LEVEL) AS PART_STR FROM DUAL " +
            "CONNECT BY REGEXP_SUBSTR(#{str},'[^,]+', 1, LEVEL) IS NOT NULL " +
            ") " +
            "SELECT A.PART_STR FROM T A " +
            "WHERE NOT EXISTS( " +
            "SELECT 1 FROM WMS_GDN_HEADER WGH  " +
            "INNER JOIN WMS_GDN_DTL WGD ON WGH.GDN_HEADER_ID = WGD.GDN_HEADER_ID " +
            "INNER JOIN WMS_PICKTASK_DTL WPD ON WGD.GDN_DTL_ID = WPD.DOC_DTL_ID " +
            "INNER JOIN WMS_PICKTASK_DTL_GOODS WPDG ON WPD.PICKTASK_DTL_ID = WPDG.PICKTASK_DTL_ID " +
            "WHERE WGH.GDN_NO = #{billNo} AND WPDG.GOODS_SN = A.PART_STR " +
            ")  ")
    List<String> selectNotExistPickTaskGoodsSn(@Param("billNo") String billNo, @Param("str") String str);

    /**
     * 根据单号/sn生成 发货单-sn的信息(包含sn,dtlId)
     *
     * @param billNo
     * @param snList
     * @return
     */
    @Select("<script>" +
            "SELECT WGD.GDN_DTL_ID,WPDG.GOODS_SN,WPDG.GOODS_ID,WG.QA_STATUS,WPDG.QTY,WPDG.COMMENTS FROM WMS_GDN_HEADER WGH \n" +
            "INNER JOIN WMS_GDN_DTL WGD ON WGH.GDN_HEADER_ID = WGD.GDN_HEADER_ID \n" +
            "INNER JOIN WMS_PICKTASK_DTL WPD ON WGD.GDN_DTL_ID = WPD.DOC_DTL_ID \n" +
            "INNER JOIN WMS_PICKTASK_DTL_GOODS WPDG ON WPD.PICKTASK_DTL_ID = WPDG.PICKTASK_DTL_ID \n" +
            "INNER JOIN WMS_GOODS WG ON WPDG.GOODS_ID = WG.GOODS_ID \n" +
            "WHERE WGH.GDN_NO = #{billNo} AND WPDG.GOODS_SN IN " +
            "<foreach collection='usedUserIdList' index='index' item='item' open='(' separator=',' close=')'>#{item}</foreach> \n" +
            "</script>")
    List<GdnDtlGoods> selectPickTaskSn2dtlId(@Param("billNo") String billNo, @Param("snList") List<String> snList);


}
